package com.naiterui.ehp.bs.doctor.vo;

import java.io.Serializable;

import javax.validation.constraints.NotEmpty;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;

import cn.hutool.core.util.IdcardUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 新备案参数VO
 */
@Data
@ApiModel(value = "RecordNewParamVO", description = "新备案参数VO")
public class RecordNewParamVO implements Serializable {

    private static final long serialVersionUID = -1257115796166451298L;

    @ApiModelProperty(value = "医生ID")
    private Long doctorId;

    @ApiModelProperty(value = "医生姓名")
    @NotEmpty(message = "医生姓名不可为空")
    private String name;

    @ApiModelProperty(value = "身份证号码")
    @NotEmpty(message = "身份证号码不可为空")
    private String idNumber;

    @ApiModelProperty(value = "身份证正面照片")
    @NotEmpty(message = "身份证正面照片不可为空")
    private String idCardUpUrl;

    @ApiModelProperty(value = "身份证反面照片")
    @NotEmpty(message = "身份证反面照片不可为空")
    private String idCardDownUrl;

    @ApiModelProperty(value = "职称证书")
    @NotEmpty(message = "职称证书照片不可为空")
    private String titleCertificateUrl;

    @ApiModelProperty(value = "医师资格证")
    private String qualificationUrl;

    @ApiModelProperty(value = "医师资格证编号")
    private String quaNumber;

    @ApiModelProperty(value = "医师资格证补充页")
    private String supplyQualificationUrl;

    @ApiModelProperty(value = "签章图片")
    private String sealImgUrl;

    /**
     * 参数校验
     */
    public void validate() throws BusinessException {

        if (this.doctorId == null || this.doctorId == 0) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (StringUtils.isEmpty(this.qualificationUrl) && StringUtils.isEmpty(this.supplyQualificationUrl)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (StringUtils.isNotBlank(this.idNumber)) {
            if (this.idNumber.length() == 18 && this.idNumber.toUpperCase().endsWith("*")) {
                this.idNumber = this.idNumber.substring(0, 17) + "X";
            }
            // 校验身份证
            if (!IdcardUtil.isValidCard(this.idNumber)) {
                throw new BusinessException(ExceptionCodes.PARAM_ERROR_ID_CARD_NUM);
            }
        }
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
